// 设置文件
import setting from "@/setting.js";
import store from "@/store";
import router from "@/router";

export default {
  namespaced: true,
  state: {
    // 布局模式(默认为 1=>上左右)
    layoutType: setting.layout.layoutType,
  },
  actions: {
    async toggle({ state, dispatch }) {
      // store 赋值
      state.layoutType = state.layoutType === 1 ? 2 : 1;
      // 跳转路由，触发菜单切换的核心逻辑(main.js)中的监听
      router.replace({
        name: "refresh",
      });
      // 持久化
      await dispatch(
        "d2admin/db/set",
        {
          dbName: "sys",
          path: "layout.layoutType",
          value: state.layoutType,
          user: true,
        },
        { root: true }
      );
    },
    async load({ state, dispatch }) {
      const { dispCss } = store.state.d2admin.sysConfig.info;

      // store 赋值
      state.layoutType = await dispatch(
        "d2admin/db/get",
        {
          dbName: "sys",
          path: "layout.layoutType",
          // defaultValue: setting.layout.layoutType,
          defaultValue: dispCss ? dispCss.layoutType : 2,
          user: true,
        },
        { root: true }
      );
    },
  },
};
